<template >
    <div class="add_con_form tl" v-loading="loading">      
            <el-dialog  :close-on-click-modal='false' :title="mytitle"  :modal-append-to-body="false" :visible.sync="visible" :show="show" :before-close="closeDialog"  @close="$emit('update:show',false)"   class="tl size_13" width="800px">                       
                <el-form :model="data"  :rules="dataYZ" ref="data" class="mar_top"  label-width="120px">                                  
                    <el-form-item label="期数" prop="stage">
                        <el-col :span="12">
                    <el-input
                        placeholder=""
                        v-model="data.stage"
                        clearable>
                    </el-input>
                        </el-col>
                    </el-form-item>
                    <el-form-item label="时间" prop="time">
                      <el-col :span="12">
                        <el-date-picker
                            style="width:100%"
                            v-model="time"
                            type="datetimerange"
                           
                            align="right"
                            unlink-panels
                            clearable
                            value-format="timestamp"
                            range-separator="-"
                            start-placeholder="起始时间"
                            end-placeholder="结束时间">
                        </el-date-picker> 
                      </el-col>
                    </el-form-item>
                    
                    <el-form-item>
                      <el-button  :loading='$store.state.isLoading' size="medium" type="primary" @click="subForm('data')">确定</el-button>
                    </el-form-item>
                </el-form>
            </el-dialog>       
       
    </div>
</template>


<style lang="less" scoped></style>

<script>
export default 
{
    data() 
    {
        return {
            options: [
            {
            value: "",
            label: "",
            }
            ],
            mytitle:"",
            fileList2:[],
            updateUrl:"",
            showImg:true,
            value:"",
            data:{
                stage:'',

            },
            loading:false,
            dataYZ:{
                stage:[
                    { required: true, message: '请输入标题', trigger: 'blur'},
                ], 
               time:[
                //    { type: 'datetime', required: true, message: '请选择日期', trigger: 'change' }
               ]
             },
             time:[],
            visible:this.show,
        }
    },
    props:{
        show:Boolean,
        editId:String,
        clickNum:Number,
    },
    methods:{
        getqihao(){
            this.loading=true;
            var params={};
            this.$api.getPlugin.getQIhao(params).then(res=>{
                console.log(res);
                if(res.data.code==200){
                   this.data.stage=res.data.result; 
                    
                }else{
                    this.$fnc.alertError(res.data.code_desc,res.data.result);
                }
                this.loading=false;
            })
        },
        uploadSuccess(response, file, fileList){
            if(response.code==200 && response.code_desc=="请求成功"){
                this.$message.success("上传成功！")
                if(response.result.indexOf("http")<=0){
                    this.showImg=true;
                    this.data.pic =response.result;
                    this.updateUrl='/api/'+response.result;
                }
            }else if(response.code==400){
                this.$message.error("上传失败！不支持的类型")
            }            
        },
        uploadError(response){
            if(response.code=400){
                this.$message.error("无效请求")
            }
        },
        getOption(){
            var data={};
            this.$api.getAdminuser.getPluginCateOption(data).then(res=>{
                this.options=res.data.result;                         
                console.log(this.options);
            })
        },
        handChange(val){
            console.log(val);
            this.data.cate=val;
        },

        closeDialog(){
            this.visible=false;
        },
        getDetails(id){      
            var data = {};
            data['id'] = id;
            this.$api.getNews.PacketDetail(data).then(res=>{
                this.data = res.data.result; 
                this.data.id = id;        
                console.log(this.data);                   
            })       
        },
        subForm(formName){
            
            this.$refs[formName].validate((valid) => 
            {  
            console.log(this.editId)
            if (valid)
            {   
                if(this.editId==undefined){
                    this.AddNews();
                }else{         
                    this.UpdateNews();
                }
            }
            });    
        },
        AddNews(){   
            var params = {};
            params['stage'] = this.data.stage;
            params['begin_time'] = this.$fnc.phptime(this.time[0]);
            params['end_time'] =  this.$fnc.phptime(this.time[1]);
            this.$api.getPlugin.guessSaveAdd(params).then(res=>{
                 if(res.data.status==1 && res.data.code == 200){
                     this.$fnc.alertSuccess({text:"添加成功"});
                     this.$emit("parent","这里刷新父页面");
                     this.visible=false;
                 }else{
                     if(res.data.result=='stage已存在'){
                         res.data.result='期数已存在';
                     }
                     this.$fnc.alertError(res.data.code_desc,res.data.result);
                 }
            });
        },
        UpdateNews(formName) 
        { 
            console.log('更新中。。');   
        this.$refs[formName].validate((valid) => 
        {  
        if (valid)
        {   
            this.data.id=this.editId;
            var params={};
                params=this.data;
                params.id=this.editId;
                params['begin_time'] = this.$fnc.phptime(this.time[0]);
                params['end_time'] =  this.$fnc.phptime(this.time[1]);

            this.$api.getNews.packetSaveEdit().then(res=>{
            if (res.data.code == 200 && res.data.result != null) {
                this.$emit("parent","这里刷新父页面");
                this.$fnc.alertSuccess({text:'编辑成功',});  //成功弹出框 
                this.visible=false;
            }else{
                this.$fnc.alertError(res.data.result);   //失败弹出框
            }
            })
        }
        });        
        },   
    },
    watch: {
        show(){
             this.getqihao();
            console.log(this.show)
            this.visible = this.show;
        },
        clickNum(id){
            console.log("点击button次数",id);
            console.log("编辑ID="+this.editId);
            if(this.editId!==undefined){
                this.mytitle = "编辑猜猜乐";
                this.getDetails(this.editId);     
            }else{
                this.mytitle = "添加期数";    
              
            }
             
        } 
    },
    mounted(){
        
    },
    created () {
        this.getqihao();
    },
    updated() {
        
    },
}    
</script>
